#include <assert.h>

#include "ILevel.h"
#include "Common.h"
#include "WLDefine.h"
#include "IOuter.h"
#include "ILogProcess.h"

namespace WLog
{

	extern ILogProcess* log;
    bool cb_trimFileName(Record* pRecord, IOuter*)
    {
        if (log->needTrim())
        {
#ifdef _WIN32
            char chSplit = '\\';
#else
            char chSplit = '/';
#endif // _WIN32
            size_t iIndex = pRecord->m_strFileName.find_last_of(chSplit);
            if (string::npos != iIndex)
            {
                pRecord->m_strFileName = pRecord->m_strFileName.substr(iIndex+1);
            }
        }
        return true;
    }

	bool cb_exit(Record*, IOuter* pOuter)/* pRecord*/
	{
		pOuter->noticeEndOut();
		(void)exit(1);
	}

	bool cb_assert(Record*, IOuter* pOuter)/* pRecord*/
	{
		/*string strSay = "File:";
		strSay += pRecord->m_strFileName;
		strSay += " : " + pRecord->m_strFuncName + "(" + l2str(pRecord->m_nLine) + ")\n\t";
		strSay += pRecord->m_strMsg + "\n";*/
		pOuter->noticeEndOut();
		assert(false);

		return true;
	}

	ILevel::ILevel(int iLevel, const char* szName, const char* szDscrp, int iSigned /*= SIGNED_OUT_ALL*/, FunCallBack cbBefore /*= cb_trimFileName*/, FunCallBack cbAfter /*= 0*/)
		: m_iLevel(iLevel), m_strName(szName), m_strDscrp(szDscrp), m_iSign(iSigned), m_callBackBefore(cbBefore), m_callBackAfter(cbAfter)
	{

	}

	ILevel::~ILevel()
	{

	}
	
	int ILevel::getLevel() const
	{
		return m_iLevel;
	}
		
	string ILevel::getName() const
	{
		return m_strName;
	}
		
	string ILevel::getDscrp() const
	{
		return m_strDscrp;
	}

	bool ILevel::bOutName() const
	{
		return SIGNED_GET_BOOL(m_iSign, OUT_NAME);
	}

	bool ILevel::bOutDscrp() const
	{
		return SIGNED_GET_BOOL(m_iSign, OUT_DSCRP);
	}

	bool ILevel::bOutLineNo() const
	{
		return SIGNED_GET_BOOL(m_iSign, OUT_LINENO);
	}

	bool ILevel::bOutFuncName() const
	{
		return SIGNED_GET_BOOL(m_iSign, OUT_FUNCNAME);
	}

	bool ILevel::bOutFileName() const
	{
		return SIGNED_GET_BOOL(m_iSign, OUT_FILENAME);
	}

	bool ILevel::bOutTime() const
	{
		return SIGNED_GET_BOOL(m_iSign, OUT_TIME);
	}

	bool ILevel::bNewLine() const
	{
		return SIGNED_GET_BOOL(m_iSign, OUT_NEWLINE);
	}
	
	bool ILevel::callBack_BeforeOut(Record* pRecord, IOuter* pOuter)
	{
		if (m_callBackBefore)
		{
			return m_callBackBefore(pRecord, pOuter);
		}
		return true;
	}

	bool ILevel::callBack_AfterOut(Record* pRecord, IOuter* pOuter)
	{
		if (m_callBackAfter)
		{
			return m_callBackAfter(pRecord, pOuter);
		}
		return true;
	}
}
